package org.mspring.platform.web.render;

import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import org.mspring.platform.utils.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Gao Youbo
 * @since Apr 16, 2012
 */
public class JSONRender extends AbstractResponseRender {

    private static final Logger log = LoggerFactory.getLogger(JSONRender.class);

    private Object content;

    public Object getContent() {
        return content;
    }

    public void setContent(Object content) {
        this.content = content;
    }

    public JSONRender() {

    }

    /**
     * @param content
     */
    public JSONRender(Object content) {
        super();
        this.content = content;
    }

    /**
     * @param content
     * @param excludesFieldsWithoutExpose
     */
    public JSONRender(Object content, boolean excludesFieldsWithoutExpose) {
        super();
        this.content = content;
    }

    @Override
    public void render(HttpServletResponse response) {
        if (this.content == null) {
            log.warn("JSON renders error, render content is null");
            throw new BusinessException("JSON renders error, render content is null");
        }
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        try {
            String JSON = content.toString();
            PrintWriter writer = response.getWriter();
            writer.println(JSON);
        } catch (Exception e) {
            log.error("JSON renders error", e);
        }
    }

}
